<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<%@ include file="/WEB-INF/jsp/include/head.jsp"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><html:text name="sys.name" /></title>

<script type="text/javascript" charset="UTF-8"
	src="<%=WEBPATH%>/dwr/engine.js?id=<%=generate%>"></script>
<script type="text/javascript" charset="UTF-8"
	src="<%=WEBPATH%>/dwr/util.js?id=<%=generate%>"></script>
<script type="text/javascript"
	src='<%=WEBPATH%>/dwr/interface/TableAction.js?id=<%=generate%>'></script>
<script type="text/javascript"
	src='<%=WEBPATH%>/dwr/interface/ColumnAction.js?id=<%=generate%>'></script>
<script type="text/javascript"
	src='<%=WEBPATH%>/dwr/interface/TableBean.js?id=<%=generate%>'></script>
<script type="text/javascript"
	src='<%=WEBPATH%>/dwr/interface/ColumnBean.js?id=<%=generate%>'></script>
</head>
<body style="overflow-y:auto;" >
<div style="width:100%;height:100%;"></div>
<table width="100%" bordercolor="#000000" cellspacing="0"
	cellpadding="0" border="0" align="center">
	<tr>
		<td width="120" height="450" valign="top">
			<div style="height:99.9%;OVERFLOW-Y:auto;"> 
				<script type="text/javascript" charset="UTF-8">
						showPageLoadingMessage("<html:text name="login.load"/>","<%=WEBPATH%>/");
						webFXTreeConfig.rootIcon		= "<%=WEBPATH%>/images/xloadtree/page_white_magnify.gif";
						webFXTreeConfig.openRootIcon	= "<%=WEBPATH%>/images/xloadtree/page_white_magnify.gif";
						webFXTreeConfig.folderIcon		= "<%=WEBPATH%>/images/xloadtree/folder.png";
						webFXTreeConfig.openFolderIcon	= "<%=WEBPATH%>/images/xloadtree/openfolder.png";
						webFXTreeConfig.fileIcon		= "<%=WEBPATH%>/images/xloadtree/page_white_gear.gif";
						webFXTreeConfig.lMinusIcon		= "<%=WEBPATH%>/images/xloadtree/Lminus.png";
						webFXTreeConfig.lPlusIcon		= "<%=WEBPATH%>/images/xloadtree/Lplus.png";
						webFXTreeConfig.tMinusIcon		= "<%=WEBPATH%>/images/xloadtree/Tminus.png";
						webFXTreeConfig.tPlusIcon		= "<%=WEBPATH%>/images/xloadtree/Tplus.png";
						webFXTreeConfig.iIcon			= "<%=WEBPATH%>/images/xloadtree/I.png";
						webFXTreeConfig.lIcon			= "<%=WEBPATH%>/images/xloadtree/L.png";
						webFXTreeConfig.tIcon			= "<%=WEBPATH%>/images/xloadtree/T.png";
						webFXTreeConfig.blankIcon		= "<%=WEBPATH%>/images/xloadtree/blank.png";
						var treeDict = new WebFXTree("<html:text name="table.entity" />",""); 
						<html:iterator value="tableChildBeans" id="tableChildBeans">
							treeDict.add(new WebFXLoadTreeItem("<html:if test="tableCName==null"><html:property value="tableName"/></html:if><html:else><html:property value="tableCName"/></html:else>", "child.crm?tableName=<html:property value="tableName"/>&tableId=<html:property value="id"/>","javascript:showTable('<html:property value="id"/>')"));
						</html:iterator>
						document.write(treeDict);
				</script>
			</div>
		</td>
		<td width="580" valign="top" id="tableButton" >
			<table align="center" width="100%">
				<tr><td colspan="2">&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>
				<tr><td colspan="2" align="right">&nbsp;&nbsp;
				<input type="button"  id="tableValueAdd" name="tableValueAdd" onclick="javascript:addTable();" value=" <html:text name="button.new"></html:text> " style="width: 90px"  >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
				</tr>
				</table>
		</td>
		<td  width="580" valign="top" id="tableValue"  style="display: none;">
			<table align="center" width="100%">
				<tr><td colspan="2">&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>
				<tr><td colspan="2" align="right">&nbsp;&nbsp;<input  type="button" onclick="javascript:tableSave();" id="tableAdd" name="tableAdd" value=" <html:text name="button.new"></html:text> "  style="width: 90px">
				<input type="button" onclick="javascript:addColumn();"  id="columnOfTableAdd" name="columnOfTableAdd" value=" 新增属性 "  style="width: 90px">
				<input type="button" onclick="javascript:tableSave();"  id="tableModify" name="tableModify" value=" <html:text name="button.modify"></html:text> " style="width: 90px"  >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>
				<tr><td><html:text name="table.tableName"/></td><td><input type="hidden" name="iflag" id="iflag"><input type="hidden" name="id" id="id"><input type="text"  name="tableName" id="tableName" style="width: 95%"
					></td></tr>
				<tr><td><html:text name="table.tableCName"/></td><td><input type="text"  name="tableCName" id="tableCName" style="width: 95%"
					></td></tr>
			</table>
		</td>
		<td  width="580" valign="top" id="columnValue" style="display: none;">
			<table align="center" width="100%">
				<tr><td colspan="4">&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>
				<tr><td colspan="4" align="right">&nbsp;&nbsp;&nbsp;&nbsp;<input type="button"  onclick="javascript:columnSave();" id="columnAdd" name="columnAdd" value=" <html:text name="button.new"></html:text> " style="width: 90px"  >
				<input type="button"  onclick="javascript:deleteColumn();" id="columnDelete" name="columnDelete" value=" <html:text name="button.delete"></html:text> " style="width: 90px"  >
				<input type="button"  id="columnModify" name="columnModify" onclick="javascript:columnSave();" value=" <html:text name="button.modify"></html:text> " style="width: 90px"  >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>
				<tr>
					<td><html:text name="column.columnName"/></td>
					<td><input type="hidden" name="columnId" id="columnId"><input type="text"  name="columnName" id="columnName" style="width: 95%" ></td>
					<td><html:text name="column.columnCName"/></td>
					<td><input type="hidden" name="tableId" id="tableId"><input type="text"  name="columnCName" id="columnCName" style="width: 95%" ></td>
				</tr>
				<tr>
					<td><html:text name="column.columnType"/></td>
					<td><select  name="columnType" id="columnType" style="width: 95%"  onchange="javascript:columnTypeChange();">
						<option value="varchar"><html:text name="global.data.varchar"/></option>
						<option value="datetime"><html:text name="global.data.datetime"/></option>
						<option value="text"><html:text name="global.data.text"/></option>
						<option value="int"><html:text name="global.data.int"/></option>
						<option value="long"><html:text name="global.data.long"/></option>
						<option value="float"><html:text name="global.data.float"/></option>
					</select>
					</td>
					<td><html:text name="column.defaultValue"/></td>
					<td><input type="text"  name="defaultValue" id="defaultValue" style="width: 95%" ></td>
					
				</tr>
				<tr>
					<td><html:text name="column.columnStyle"/></td>
					<td><select  name="columnStyle" id="columnStyle" style="width: 95%" >
						<option value="text"><html:text name="global.form.text"/></option>
						<option value="textarea"><html:text name="global.form.textarea"/></option>
						<option value="file"><html:text name="global.form.file"/></option> 
					</select>
					</td>
					<td><html:text name="column.columnNull"/></td>
					<td><select  name="columnNull" id="columnNull" style="width: 95%" >
						<option value="0"><html:text name="entity.no"/></option>
						<option value="1"><html:text name="entity.yes"/></option>
					</select>
					</td>
				</tr>
				<tr>
					<td><html:text name="column.length"/></td>
					<td><input type="text"  name="length" id="length" style="width: 95%" >
					</td>
					<td><html:text name="column.orderBy"/></td>
					<td><input type="text"  name="orderBy" id="orderBy" style="width: 95%" >
					</td>
				</tr>
				<!-- td><html:text name="column.isList"/></td><td><input type="text"  name="isList" id="isList" style="width: 95%"
					></td></tr>
				<tr><td><html:text name="column.isQuery"/></td><td colspan="3"><input type="text"  name="isQuery" id="isQuery" style="width: 95%"
					></td></tr> -->
			</table>
		</td>
	</tr>
</table>  

<script type="text/javascript"> 
window.onload=hiddenPageLoadingMessage;
/************table action start******/
function tableSave(){
	var tableValue=jQuery('#tableName').attr("value");
	if(tableValue==""){
		alert("<html:text name="errors.required"><html:param><html:text name="table.tableName"/></html:param></html:text>");
		return false;
	}
	var TableBean={
		id: jQuery('#id').attr("value"),
		tableName: jQuery('#tableName').attr("value"),
		tableCName: jQuery('#tableCName').attr("value")
	};
	TableAction.dwrSave(TableBean,saveTable);
} 

function saveTable(data){
	if(data.indexOf("success")>=0){
		if(jQuery('#id').attr("value")==""){
			alert("<html:text name="entity.saved"/>");
		}else{
			alert("<html:text name="entity.updated"/>");
		}
		jQuery('#columnOfTableAdd').hide();
		jQuery('#tableModify').hide();
		jQuery('#tableAdd').hide();
		window.location="index.crm";
	}else{
		if(jQuery('#id').attr("value")==""){
			alert("<html:text name="entity.saved.error"/>");
		}else{
			alert("<html:text name="entity.updated.error"/>");
		}
	}
}
function addTable(){
	jQuery('#tableValue').show();
	jQuery('#tableButton').hide();
	jQuery('#columnOfTableAdd').hide();
	
	jQuery('#tableAdd').show();
	jQuery('#tableModify').hide();
	jQuery('#columnValue').hide(); 
	
	jQuery('#id').val("");
	jQuery('#tableCName').val("");
	jQuery('#tableName').val("");
	jQuery('#tableName').removeAttr('disabled');
} 

function showTable(id){ 
	TableAction.dwrView(id,updateTable);
}
function updateTable(data){
	jQuery('#id').val(data.id);
	jQuery('#tableCName').val(data.tableCName);
	jQuery('#tableName').val(data.tableName);
	jQuery('#tableName').attr('disabled','true');
	
	jQuery('#tableModify').show();
	jQuery('#tableAdd').hide();
	jQuery('#columnOfTableAdd').show();
	
	jQuery('#tableValue').show();
	jQuery('#tableButton').hide();
	jQuery('#columnValue').hide();
	
}
function deleteTable(){
	//alert(jQuery('#columnId').attr("value"));
	if(confirm("<html:text name="table.deleted"/>"))
		TableAction.dwrDelete(jQuery('#id').attr("value"),tableDelete);
}
function tableDelete(data){
	//alert(data);
	if(data=="success"){
		alert("<html:text name="entity.deleted"/>");
		window.location="index.crm";
	}else{
		alert("<html:text name="entity.deleted.error"/>");
	}
}
/************table action end******/

/************column action start******/
function deleteColumn(){
	//alert(jQuery('#columnId').attr("value"));
	if(confirm("<html:text name="column.deleted"/>"))
	ColumnAction.dwrDelete(jQuery('#columnId').attr("value"),columnDelete);
}
function columnDelete(data){
	//alert(data);
	if(data=="success"){
		alert("<html:text name="entity.deleted"/>");
		window.location="index.crm";
	}else{
		alert("<html:text name="entity.deleted.error"/>");
	}
}
function columnTypeChange(){
	if(jQuery('#columnType').attr("value")!="varchar"){
		jQuery('#length').val("");
		jQuery('#length').attr('disabled','true');
	}else{ 
		jQuery('#length').removeAttr('disabled');
	}
}
function addColumn(){
	jQuery('#columnId').val("");
	jQuery('#columnCName').val("");
	 
	jQuery('#columnName').val(""); 
	 
	
	jQuery('#iflag').val("1"); 
	jQuery('#tableId').val(jQuery('#id').attr("value"));
	jQuery('#columnType').val(""); 
	
	jQuery('#columnNull').val("");
	jQuery('#columnStyle').val("");
	jQuery('#defaultValue').val("");
	jQuery('#length').val("");
	jQuery('#orderBy').val("");
	
	jQuery('#columnCName').removeAttr('disabled');
	jQuery('#columnName').removeAttr('disabled');
	jQuery('#columnType').removeAttr('disabled'); 
	jQuery('#columnNull').removeAttr('disabled');
	jQuery('#columnStyle').removeAttr('disabled');
	jQuery('#defaultValue').removeAttr('disabled'); 
	jQuery('#length').removeAttr('disabled');
	
	jQuery('#columnModify').hide();
	jQuery('#columnDelete').hide();
	jQuery('#columnAdd').show();
	
	
	jQuery('#columnValue').show();
	jQuery('#tableValue').hide();
	jQuery('#tableButton').hide();
}
function saveColumn(data){
	if(data.indexOf("success")>=0){
		if(jQuery('#columnId').attr("value")==""){
			alert("<html:text name="entity.saved"/>");
		}else{
			alert("<html:text name="entity.updated"/>");
		}
		showTable(jQuery('#tableId').attr("value"));
		
		//window.location="index.crm";
	}else{
		if(jQuery('#columnId').attr("value")==""){
			alert("<html:text name="entity.saved.error"/>");
		}else{
			alert("<html:text name="entity.updated.error"/>");
		}
	}
}
function columnSave(){
	var TableBean={id:jQuery('#tableId').attr("value")};
	//alert(jQuery('#tableId').attr("value"));
	var columnNull=jQuery('#columnNull').attr("value");
	var columnStyle=jQuery('#columnStyle').attr("value");
	var tableValue=jQuery('#columnName').attr("value");
	if(tableValue==""){
		alert("<html:text name="errors.required"><html:param><html:text name="column.columnName"/></html:param></html:text>");
		return false;
	}
	if(jQuery('#columnType').attr("value")=="varchar"){
		if(jQuery('#length').attr("value")==""||jQuery('#length').attr("value")<0){
			alert("<html:text name="errors.required"><html:param><html:text name="column.defaultValue"/></html:param></html:text> ");
			return false;
		}
	}
	if(columnNull=="1"){
		columnNull="YES";
	}else{
		columnNull="NO";
	}  
	var ColumnBean={
		id : jQuery('#columnId').attr("value"),
		columnCName : jQuery('#columnCName').attr("value"),
		columnName : jQuery('#columnName').attr("value"),
		columnType : jQuery('#columnType').attr("value"),
		orderBy : jQuery('#orderBy').attr("value"),
		columnNull : columnNull,
		columnStyle : columnStyle,
		defaultValue : jQuery('#defaultValue').attr("value"),
		length : jQuery('#length').attr("value"),
		iflag : jQuery('#iflag').attr("value"),
		tableBean : TableBean 
	};
	ColumnAction.dwrSave(ColumnBean,saveColumn);
}
function showCategoryValue(id,tableId){
	ColumnAction.dwrView(id,tableId,updateColumn);
}
function updateColumn(data){
	jQuery('#columnId').val(data.id);
	jQuery('#columnCName').val(data.columnCName);
	jQuery('#columnName').val(data.columnName);
	
	jQuery('#tableId').val(data.tableBean.id);
	jQuery('#columnType').val(data.columnType);
	jQuery('#orderBy').val(data.orderBy);
	jQuery('#iflag').val(data.iflag);
	//alert(data.columnNull);
	if(data.columnNull=="NO"){
		jQuery('#columnNull').val('0'); 
	}else{
		jQuery('#columnNull').val('1');
	}  

	var key=data.columnStyle;
	if(key==null){
		key="";
	}
	key=jQuery.trim(key);
	//alert(key);
	if(key==""){ 
		jQuery('#columnStyle').val('text'); 
	}else{
		jQuery('#columnStyle').val(key); 
	} 
	
	jQuery('#defaultValue').val(data.defaultValue);
	jQuery('#length').val(data.length);

	
	jQuery('#columnModify').show();
	jQuery('#columnAdd').hide();
	
	jQuery('#columnValue').show();
	jQuery('#tableValue').hide();
	jQuery('#tableButton').hide();
	if(jQuery('#iflag').attr("value")=="0"){
		jQuery('#columnDelete').hide();
		jQuery('#columnName').attr('disabled','true');
		jQuery('#columnType').attr('disabled','true');
		jQuery('#columnNull').attr('disabled','true');
		jQuery('#columnStyle').attr('disabled','true');
	}else{
		jQuery('#columnDelete').show();
		jQuery('#columnName').attr('disabled','true');
		jQuery('#columnType').attr('disabled','true');
		jQuery('#columnNull').attr('disabled','true');
		jQuery('#columnCName').removeAttr('disabled');  
		jQuery('#columnStyle').removeAttr('disabled');
		jQuery('#defaultValue').removeAttr('disabled'); 
	}
	columnTypeChange();
}
</script>
</body>
</html>